var t = require("../../api.js"),
    a = getApp();
Page({
    data: {
        cat_id: "",
        list: [],
        page: 1,
        no_more: !1,
        loading: !1,
        page_count: 1,
        longitude: "",
        latitude: "",
        score: [ 1, 2, 3, 4, 5 ],
        keyword: ""
    },
    onLoad: function(t) {
        a.pageOnLoad(this), t.cat_id && (this.data.cat_id = t.cat_id), this.loadShopList()
        
    },
    loadShopList: function(i) {        
        var o = this;
        o.data.no_more ? "function" == typeof i && i() : o.data.loading ? console.log("nomore") : (o.setData({
            loading: !0
        }),wx.getLocation({
            success: function(t) {
                o.setData({
                    longitude: t.longitude,
                    latitude: t.latitude
                });
            },
            complete: function() {
                o.loadData();
            }
        }))
    },
    loadData:function() {
        var o = this;
        console.log(o)
        a.request({
            url: t.mch.shop_list,
            data: {
                keyword: o.data.keyword,
                cat_id: o.data.cat_id,
                page: o.data.page,
                longitude: o.data.longitude,
                latitude: o.data.latitude
            },
            success: function(t) {
                console.log(t)
                if (0 == t.code) {
                    if (!t.data.list || !t.data.list.length) return void o.setData({
                        no_more: !0,
                        cat_list: t.data.cat_list
                    });
                  for (var i in t.data.list) {
                    if (t.data.list[i]['tel']) {
                      t.data.list[i]['tel'] = t.data.list[i]['tel'].substr(0, 3) + '****' + t.data.list[i]['tel'].substr(7);
                    } else {
                      t.data.list[i]['tel'] = '';
                    }
                  }
                    o.data.list || (o.data.list = []), o.data.list = o.data.list.concat(t.data.list), o.setData({
                        list: o.data.list,
                        page: o.data.page + 1,
                        cat_list: t.data.cat_list
                    })
                }
            },
            complete: function() {
                o.setData({
                    loading: !1
                }), "function" == typeof i && i()
            }
        })
    },
    onReady: function() {
        a.pageOnReady(this)
    },
    onShow: function() {
        a.pageOnShow(this)
    },
    onHide: function() {
        a.pageOnHide(this)
    },
    onUnload: function() {
        a.pageOnUnload(this)
    },
    onPullDownRefresh: function() {
        var a = this;
        a.setData({
            keyword: "",
            page: 1
        }), wx.getLocation({
            success: function(t) {
                a.setData({
                    longitude: t.longitude,
                    latitude: t.latitude
                });
            },
            complete: function() {
                a.loadData(), wx.stopPullDownRefresh();
            }
        });
    },
    onReachBottom: function() {
        this.loadShopList()
    },
    showCatList: function() {
        this.setData({
            show_cat_list: !0
        })
    },
    hideCatList: function() {
        this.setData({
            show_cat_list: !1
        })
    },
    go: function(t) {
        var a = t.currentTarget.dataset.index;
        console.log(a);
        wx.navigateTo({
            url: "/mch/shop/shop?mch_id=" + a
        });
    },
    goto: function(a) {
        var e = this;
        wx.getSetting({
            success: function(t) {
                t.authSetting["scope.userLocation"] ? e.location(a) : app.getauth({
                    content: "需要获取您的地理位置授权，请到小程序设置中打开授权！",
                    cancel: !1,
                    success: function(t) {
                        t.authSetting["scope.userLocation"] && e.location(a);
                    }
                });
            }
        });
    },
    location: function(t) {
        var a = t.currentTarget.dataset.index, e = this.data.list;
        wx.openLocation({
            latitude: parseFloat(e[a].latitude),
            longitude: parseFloat(e[a].longitude),
            name: e[a].name,
            address: e[a].address
        });
    },
    inputFocus: function(t) {
        this.setData({
            show: !0
        });
    },
    inputBlur: function(t) {
        this.setData({
            show: !1
        });
    },
    inputConfirm: function(t) {
        this.search();
    },
    input: function(t) {
        this.setData({
            keyword: t.detail.value
        });
    },
    search: function(o) {
        var that = this;
        wx.showLoading({
            title: "搜索中"
        }), a.request({
            url: t.mch.shop_list,
            data: {
                keyword: that.data.keyword,
                longitude: that.data.longitude,
                latitude: that.data.latitude
            },
            success: function(s) {
                0 == s.code && that.setData(s.data);
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
});